package eu.goldenak.ircbot;

import eu.goldenak.ircbot.BotGroup.DistributeMessage;
import java.io.PrintWriter;

/**
 * This class will handle all communication between the IRC server and the 
 * IrcBot class.
 */
public class IrcHandler {
	/**
	 * And a pointer to the PrintWriter which we will be needed.
	 */
	private PrintWriter m_pOut;
	
	/**
	 * In order to easily send formatted messages, we need a pointer to the 
	 * PrintWriter object, which is supplied to the constructor.
	 */
	public IrcHandler (PrintWriter pWriter) {
		m_pOut = pWriter;
	}
	
	/**
	 * Join a channel.
	 */
	public void join (String sChannel) {
		send ("JOIN " + sChannel);
	}
	
	/**
	 * Join a channel with key.
	 */
	public void join (String sChannel, String sKey) {
		send ("JOIN " + sChannel + " " + sKey);
	}
	
	/**
	 * This method lets you set a mode on yourself or a channel.
	 */
	public void mode (String sDestination, String sMode) {
		send ("MODE " + sDestination + " " + sMode);
	}
	
	/**
	 * Send a NOTICE.
	 */
	public void notice (String sDestination, String sMessage) {
		send ("NOTICE " + sDestination + " :" + sMessage);
	}
	
	/**
	 * Send a PRIVMSG, this will be used the most.
	 */
	public void privmsg (String sDestination, String sMessage) {
		send ("PRIVMSG " + sDestination + " :" + sMessage);
	}
	
	/**
	 * Send a distributed message.
	 */
	public void privmsg (DistributeMessage pMessage) {
		send ("PRIVMSG " + pMessage.channel + " :" + pMessage.message);
	}
	
	/**
	 * Say byebye to the IRC server.
	 */
	public void quit (String sMessage) {
		send ("QUIT :" + sMessage);
	}
	
	/**
	 * Puts a line onto the socket.
	 */
	public void send (String sLine) {
		m_pOut.println (sLine);
		//System.out.println (sLine);
	}
	
	/**
	 * Closes the writer.
	 */
	public void close () {
		try {
			m_pOut.close();
			m_pOut = null;
		} catch (Exception e) {}
	}
}